package com.hn.shopadmin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hn.shopadmin.service.DictMasterService;
import com.hn.kiss.modules.sys.shopdb.entity.DictDetailExample;
import com.hn.kiss.modules.sys.shopdb.entity.DictMaster;
import com.hn.kiss.modules.sys.shopdb.entity.DictMasterExample;
import com.hn.kiss.modules.sys.shopdb.mapper.DictDetailMapper;
import com.hn.kiss.modules.sys.shopcommon.common.ParmException;
import com.hn.kiss.modules.sys.shopdb.mapper.DictMasterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictMasterServiceimpl implements DictMasterService {

    @Autowired
    DictMasterMapper dictMasterMapper;
    @Autowired
    DictDetailMapper dictDetailMapper;

    @Override
    public int add(DictMaster dictMaster) throws ParmException {
        //代码转化为大写
        dictMaster.setTypeCode(dictMaster.getTypeCode().toUpperCase());
        //校验模板是否合法
        boolean b = checkDictMaster(dictMaster);
        return dictMasterMapper.insert(dictMaster);
    }

    @Override
    public int del(String typeCode) {
        DictDetailExample dictDetailExample = new DictDetailExample();
        dictDetailExample.createCriteria().andTypeCodeEqualTo(typeCode);
        dictDetailMapper.deleteByExample(dictDetailExample);
        return dictMasterMapper.deleteByPrimaryKey(typeCode);
    }

    @Override
    public int update(DictMaster dictMaster) throws ParmException {
        //代码转化为大写
        dictMaster.setTypeCode(dictMaster.getTypeCode().toUpperCase());

        DictMaster byId = getById(dictMaster.getTypeCode());
        //与原名称不相同,校验是否重复
        if ( ! byId.getTypeName().equals(dictMaster.getTypeName())){
            boolean b = checkTypeName(dictMaster.getTypeName());
            if (b){
                throw new ParmException("字典类别名称已存在");
            }
        }
        return dictMasterMapper.updateByPrimaryKey(dictMaster);
    }

    @Override
    public DictMaster getById(String typeCode) {
        return dictMasterMapper.selectByPrimaryKey(typeCode);
    }

    @Override
    public PageInfo<DictMaster> listPage(Integer page, Integer limit, String keywork) {
        PageHelper.startPage(page, limit);
        if (keywork == null){
            keywork = "%%";
        }else{
            keywork = "%" + keywork.trim() + "%";
        }

        // typeCode || typeName
        DictMasterExample dictMasterExample = new DictMasterExample();
        dictMasterExample.createCriteria().andTypeCodeLike(keywork);
        dictMasterExample.or().andTypeNameLike(keywork);

        List<DictMaster> dictMasters = dictMasterMapper.selectByExample(dictMasterExample);
        return new PageInfo<DictMaster>(dictMasters);
    }

    /**
     * 校验字典主记录是否合法
     */
    boolean checkDictMaster(DictMaster dictMaster) throws ParmException {
        //校验字典类别代码是否存在
        boolean code = checkTypeCode(dictMaster.getTypeCode());
        if(code){
            throw new ParmException("字典代码已存在");
        }
        //检查字典类别名称是否存在
        boolean name = checkTypeName(dictMaster.getTypeName());
        if(name){
            throw new ParmException("字典名称已存在");
        }

        return true;
    }

    /**
     * 校验字典类别代码是否存在
     * @param typeCode
     * @return
     */
    boolean checkTypeCode(String typeCode){
        DictMaster dictMaster = dictMasterMapper.selectByPrimaryKey(typeCode);
        if (dictMaster == null){
            return false;
        }
        return true;
    }

    /**
     * 校验字典类别名称存在
     * @param typeName
     * @return
     */
    boolean checkTypeName(String typeName){
        DictMasterExample dictMasterExample = new DictMasterExample();
        dictMasterExample.createCriteria().andTypeNameEqualTo(typeName);
        List<DictMaster> dictMasters = dictMasterMapper.selectByExample(dictMasterExample);

        //字典名称不存
        if (dictMasters == null || dictMasters.size()==0){
            return false;
        }

        return true;
    }


}
